//
//  WFPhotoLayouy.m
//  谷居
//
//  Created by dllo on 16/7/4.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFPhotoLayouy.h"
#define WIDTH self.collectionView.frame.size.width

@interface WFPhotoLayouy ()
@property (nonatomic, retain) NSMutableArray *heightArray;

@property (nonatomic, retain) NSMutableArray *attribtesArray;
@end
@implementation WFPhotoLayouy
- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.attribtesArray = [NSMutableArray array];
    }
    return self;
}



- (void)prepareLayout {
    [super prepareLayout];
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    NSLog(@"count: %ld", count);

    for (int i = 0; i < count; i++) {
        
        /** 遍历每个item. */
        [self setItemFrame:i];
    }
}
- (void)setItemFrame:(NSInteger)index {
//    __block NSString *
    
    /** 创建一个indexPaTH. */
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    CGFloat width = WIDTH;
    CGFloat x = SCREEN_WIDTH * index;
    CGFloat layoutheigt = [self.delegate layout:self heightForItemAtIndexPath:indexPath Width:width];
    CGFloat y = -layoutheigt / 2 + self.collectionView.frame.size.height / 2;
    
    /** 设置item的frame*/
    
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    layoutAttributes.frame = CGRectMake(x, y, width, layoutheigt);
    [self.attribtesArray addObject:layoutAttributes];
}

#pragma mark - 将所有样式返回, 告诉系统如何布局
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attribtesArray;
}
#pragma mark - 设置滚动范围
- (CGSize)collectionViewContentSize {
    return CGSizeMake(SCREEN_WIDTH * self.count, 0);
}


@end
